package com.fu.web.serviceImplments;

import com.fu.web.DTO.docLabel;
import com.fu.web.DTO.payVue;
import com.fu.web.DTO.registerVue;
import com.fu.web.entity.*;
import com.fu.web.mapper.*;
import com.fu.web.service.menuService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class menuServiceImpl implements menuService {
    @Autowired
    private registerFormMapper registerFormMapper;

    @Autowired
    private chargeFormMapper chargeFormMapper;

    @Autowired
    private departmentsMapper departmentsMapper;

    @Autowired
    private doctorMapper doctorMapper;

    @Autowired
    private patientMapper patientMapper;
    @Autowired
    private descbookMapper descbookMapper;

    @Override
    public void addRegisterForm(registerForm registerForm, registerVue registerVue) {
        patient patient = patientMapper.selectByPrimaryKey(registerVue.getPatientId());
        if(patient==null){

            patient=new patient(registerVue.getPatientId(),
                    registerVue.getPatientName(),
                    registerVue.getAddress(),
                    registerVue.getTel(),
                    registerVue.getAge(),
                    registerVue.getCompany());
            patientMapper.insert(patient);
        }


        registerForm=new registerForm();
        registerForm.setRegisterfromId(null);
        registerForm.setIsDel(0);
        registerForm.setCost(registerVue.getRegisCost());

        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = format.format(date);
        registerForm.setCreateTime(strDate);
        doctor doctor = doctorMapper.selectbyName(registerVue.getDoctorName());
        registerForm.setDoctorId(doctor.getDoctorId());
        registerForm.setIsOver("0");
        registerForm.setPatientId(registerVue.getPatientId());
        descbook descbookTemp=new descbook();
        descbookTemp.setDocid(registerForm.getDoctorId());
        descbookTemp.setAllergyHis("");
        descbookTemp.setPastHis("");
        descbookTemp.setBookId(null);

        descbookTemp.setDesc("");
        descbookTemp.setPatiid(registerForm.getPatientId());
        descbookTemp.setIsOver(0);
        descbookTemp.setPresId(null);
        System.out.println(descbookTemp.toString());

        registerFormMapper.insert(registerForm);
       List<registerForm> registerForm1 = registerFormMapper.selectMax();
        com.fu.web.entity.registerForm re02=registerForm1.get(registerForm1.size()-1);
        descbookTemp.setRegisterformId(re02.getRegisterfromId());
        System.out.println(descbookTemp.toString());
        descbookMapper.inset(descbookTemp);
    }
    @Override
    public void carryRollRegister(registerForm registerForm) {
        registerForm.setIsDel(1);
        registerFormMapper.updateByPrimaryKey(registerForm);
    }

    @Override
    public payVue payTotalCost(Integer chargeId) {
        chargeForm chargeForm1 = chargeFormMapper.selectByPrimaryKey(chargeId);

        if(chargeForm1.getIsover()!=1){
            payVue payVue=new payVue();
            payVue.setPatientId(chargeForm1.getChargepatientId());
            patient patient = patientMapper.selectByPrimaryKey(chargeForm1.getChargepatientId());
            payVue.setAddress(patient.getAddress());
            payVue.setAge(patient.getAge());
            payVue.setTel(patient.getTel());
            payVue.setCompany(patient.getCompany());
            payVue.setPatientName(patient.getPatientName());
            payVue.setDrugTotalPrice(chargeForm1.getTotalPrice());

            descbook select = descbookMapper.select(chargeForm1.getBookid());
            doctor doctor = doctorMapper.selectByPrimaryKey(select.getDocid());
            payVue.setDoctorName(doctor.getDoctorName());
            departments departments = departmentsMapper.selectByPrimaryKey(doctor.getDepartmentId());
            payVue.setDepartmentName(departments.getDepartmentName());
            chargeForm1.setIsover(1);
            chargeFormMapper.updateByPrimaryKey(chargeForm1);
            System.out.println(payVue.toString());
            return payVue;
        }
        else {
            return null;
        }

    }

    @Override
    public void RollpayTotalCost(chargeForm chargeForm) {
        chargeForm.setIsDelete(1);
        chargeFormMapper.updateByPrimaryKey(chargeForm);

    }

    @Override
    public List<docLabel> showDoc() {
        List<doctor> doctors = new ArrayList<doctor>();
        doctors=doctorMapper.showALl();
        List<docLabel> list=new ArrayList<docLabel>();
        for (doctor doctor : doctors) {
            String tempCost="";
            docLabel temp=new docLabel();
            temp.setDecName(doctor.getDoctorName());
            temp.setDepartId(doctor.getDepartmentId());
            if(doctor.getDoctorLevel()==1){
                tempCost="10";
            }
            else if(doctor.getDoctorLevel()==2){
                tempCost="20";
            }
            else if(doctor.getDoctorLevel()==3){
                tempCost="30";
            }
            temp.setCost(tempCost);
            temp.setId(doctor.getDoctorId());
            list.add(temp);

        }
        return list;
    }

    @Override
    public List<departments> showDe() {
        List<departments> list=new ArrayList<departments>();
        list=departmentsMapper.selectAll();
        return list;
    }

    @Override
    public void addDescBook(descbook descbook) {

    }
}
